package org.jeecgframework.web.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.ZyInstallRecordServiceI;
import org.jeecgframework.web.service.ZyTimeTaskForInstallRecordSevice;
import org.jeecgframework.web.service.impl.ZyTimeTaskForIntallRecordServiceImpl;
import org.jeecgframework.web.system.pojo.base.ZyInstallRecordCpEntity;
import org.jeecgframework.web.system.pojo.base.ZyInstallRecordEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: 安装记录
 * @author ZhangYunlong
 * @date 2016-03-29 16:16:08
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zyInstallRecordController")
public class ZyInstallRecordController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZyInstallRecordController.class); // NOPMD by dzqThinkpad on 16-4-1 下午3:02

	@Autowired
	private ZyInstallRecordServiceI zyInstallRecordService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}



	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ZyInstallRecordEntity zyInstallRecord,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyInstallRecordEntity.class, dataGrid);
		String sn = request.getParameter("sn");
		if(null!=sn&&!sn.equals("")){
			cq.eq("sn", sn);
		}
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyInstallRecord, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zyInstallRecordService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	//去重用
	@RequestMapping(params = "datagridForEquipmentSpareView")
	public void datagridForEquipmentSpareView(ZyInstallRecordCpEntity zyInstallRecordCp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyInstallRecordCpEntity.class, dataGrid);
		String sn = request.getParameter("sn");
		if(null!=sn&&!sn.equals("")){
			cq.eq("sn", sn);
		}
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyInstallRecordCp, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zyInstallRecordService.getDataGridReturn(cq, true);
		List<ZyInstallRecordCpEntity> list = (List<ZyInstallRecordCpEntity>)dataGrid.getResults();
		for(ZyInstallRecordCpEntity zyInstallRecordCpEntity:list){
			String temp = zyInstallRecordCpEntity.getVersion().substring(1);
			if(Integer.valueOf(temp)>2&&
					Float.valueOf(zyInstallRecordCpEntity.getVersion())%2==1){
				list.remove(zyInstallRecordCpEntity);
			}
		}
		TagUtil.datagrid(response, dataGrid);
	}
	
	
	/**
	 * @author ZhangYunlong
	 * 2016-4-29 下午5:59:53
	 * Description 跳转安装记录差异历史页面 跳转
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "zyInstallRecordCpHis")
	public ModelAndView zyInstallRecordCpHis(HttpServletRequest request) {
		return new ModelAndView("com/zybk/zy_upload_install_record/zyInstallRecordCpHis");
	}
	
	/**
	 * @author xiul
	 * 2016-4-14 下午5:59:53
	 * Description 跳转安装记录差异页面 跳转
	 */
	@RequestMapping(params = "zyInstallRecordCp")
	public ModelAndView zyInstallRecordCp(HttpServletRequest request) {
		return new ModelAndView("com/zybk/zy_upload_install_record/zyInstallRecordCp");
	}
	
	/**
	 * @author xiul
	 * 2016-4-14 下午6:00:15
	 * Description 安装记录差异页面 datagrid  
	 */
	@RequestMapping(params = "differDatagrid")
	public void differDatagrid(ZyInstallRecordCpEntity zyInstallRecordCp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyInstallRecordCpEntity.class, dataGrid);
		//查询条件组装器
		cq.eq("status", "0");
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyInstallRecordCp, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		String temp = "";
		for(ZyInstallRecordCpEntity zyInstallRecordCpEntity :
			(List<ZyInstallRecordCpEntity>)dataGrid.getResults()){
			temp = zyInstallRecordCpEntity.getVersion().substring(1);
			if(Float.valueOf(temp)%2==0){
				zyInstallRecordCpEntity.setFormFlag("2");
			}
			if(zyInstallRecordCpEntity.getType().equals("d")||zyInstallRecordCpEntity.getType().equals("i")){
				zyInstallRecordCpEntity.setFormFlag("2");
			}
		};
		
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * @author xiul
	 * 2016-4-14 下午6:00:15
	 * Description 安装记录差异页面 datagrid  
	 * @param zyInstallRecordCp
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "differHisDatagrid")
	public void differHisDatagrid(ZyInstallRecordCpEntity zyInstallRecordCp,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyInstallRecordCpEntity.class, dataGrid);
		//查询条件组装器
		cq.eq("status", "1");
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyInstallRecordCp, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 更新安装记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doSetStatus")
	@ResponseBody
	public AjaxJson doSetStatus(ZyInstallRecordCpEntity zyInstallRecordCp, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "安装记录处理成功";
		zyInstallRecordCp = systemService.get(ZyInstallRecordCpEntity.class, zyInstallRecordCp.getId());
		List<ZyInstallRecordCpEntity> zyInstallRecordCps = systemService.findByProperty(ZyInstallRecordCpEntity.class,"sn",zyInstallRecordCp.getSn());
		try {
//			MyBeanUtils.copyBeanNotNull2Bean(zyInstallRecordCp, t);
			for(ZyInstallRecordCpEntity zyInstallRecordCpEntity:zyInstallRecordCps){
				zyInstallRecordCpEntity.setStatus("1");
				zyInstallRecordService.saveOrUpdateForCp(zyInstallRecordCpEntity);
			}
//			zyInstallRecordCp.setStatus("1");
//			zyInstallRecordService.saveOrUpdateForCp(zyInstallRecordCp);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "安装记录处理失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	
	/**
	 * 删除安装记录
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ZyInstallRecordEntity zyInstallRecord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		zyInstallRecord = systemService.getEntity(ZyInstallRecordEntity.class, zyInstallRecord.getId());
		message = "安装记录删除成功";
		try{
			zyInstallRecordService.delete(zyInstallRecord);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "安装记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除安装记录
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "安装记录删除成功";
		try{
			for(String id:ids.split(",")){
				ZyInstallRecordEntity zyInstallRecord = systemService.getEntity(ZyInstallRecordEntity.class, 
				id
				);
				zyInstallRecordService.delete(zyInstallRecord);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "安装记录删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加安装记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(ZyInstallRecordEntity zyInstallRecord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "安装记录添加成功";
		try{
			zyInstallRecordService.save(zyInstallRecord);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "安装记录添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 更新安装记录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(ZyInstallRecordEntity zyInstallRecord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "安装记录更新成功";
		ZyInstallRecordEntity t = zyInstallRecordService.get(ZyInstallRecordEntity.class, zyInstallRecord.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(zyInstallRecord, t);
			zyInstallRecordService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "安装记录更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 安装记录新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(ZyInstallRecordEntity zyInstallRecord, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(zyInstallRecord.getId())) {
			zyInstallRecord = zyInstallRecordService.getEntity(ZyInstallRecordEntity.class, zyInstallRecord.getId());
			req.setAttribute("zyInstallRecordPage", zyInstallRecord);
		}
		return new ModelAndView("com/zybk/zy_install_record/zyInstallRecord-add");
	}
	/**
	 * 安装记录编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(ZyInstallRecordEntity zyInstallRecord, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(zyInstallRecord.getId())) {
			zyInstallRecord = zyInstallRecordService.getEntity(ZyInstallRecordEntity.class, zyInstallRecord.getId());
			req.setAttribute("zyInstallRecordPage", zyInstallRecord);
		}
		return new ModelAndView("com/zybk/zy_install_record/zyInstallRecord-update");
	}
	
	/**-------------测试用------begin-------*/
	@Autowired
	private ZyTimeTaskForInstallRecordSevice zyTimeTaskForInstallRecordSericesImpl;
	
	/**
	 * @author xiul
	 * 2016-7-7 下午5:23:49
	 * Description 方便测试，跑定时导入安装记录任务用
	 */
	@ResponseBody
	@RequestMapping(params="runTimeTask")
	public void runTimeTask(){
		zyTimeTaskForInstallRecordSericesImpl.zyRecordImport();
	}
	/**-------------测试用-------end------*/
}
